local BulletCfg = require "BulletCfg"
local BulletTypeCfg = require "BulletTypeCfg"

local EnemyPlane = {}
local t_concat = table.concat
local t_rm 	= table.remove
local t_sort = table.sort
local math_ceil = math.ceil

local e_plane_mt = {__index = EnemyPlane}

local player_plane_size = {
	width = 186,
	height = 130,
}
 
 local enemy_plane_size = {
	width = 120,
	height = 79,
 }
 
 local bullet_size = {
	width = 62,
	height = 108,
 }

function EnemyPlane:ctor(idx)
	self._idx = idx
	self._posX = 0;
	self._posY = 0;
	self._speed = 0;
	self._width = 0;
	self._height = 0;
	self._angle = -90;
	self._blood = 100;
	self._behavior = 0;
	self._endposX = 0;
	self._endposY = 0;
	self._attack = 0;
	self._defensive = 0;
	
	-- 动作
	self._actionarray = {};
	self._actionstep = 0;
	self._steptime = 0;
	self._bFire = false;
	self._nFireTime = 0;
	self._bulletdata = {};
end

function EnemyPlane:getInfo()
	return {
		idx = self._idx,
		posX = self._posX,
		posY = self._posY,
		speed = self._speed,
		width = self._width,
		height = self._height,
		angle = self._angle,
		blood = self._blood,
		behavior = self._behavior,
		endposX = self._endposX,
		endposY = self._endposY,
		attack = self._attack,
		defensive = self._defensive,
	}
end

function EnemyPlane:setPositon(pos)
	self._posX = pos.x or 0;
	self._posY = pos.y or 0;
end

function EnemyPlane:getPosition()
	return 
	{
		nXpos = self._posX, 
		nYpos = self._posY,
	};
end

function EnemyPlane:setEndPositon(pos)
	self._endposX = pos.x or 0;
	self._endposY = pos.y or 0;
end

function EnemyPlane:getEndPosition()
	return 
	{
		nXpos = self._endposX, 
		nYpos = self._endposY,
	};
end

function EnemyPlane:setSpeed(speed)
	self._speed = speed;
end

function EnemyPlane:getSpeed()
	return self._speed;
end

function EnemyPlane:setAngle(angle)
	self._angle = angle;
end

function EnemyPlane:getAngle()
	return self._angle;
end

function EnemyPlane:setActionArray(action)
	self._actionarray = action or {}
	
	if action and #action > 0 then
		self._posX = action[1].startpos.x or 0;
		self._posY = action[1].startpos.y or 0;
		self._angle = action[1].angle or 0;
	end
end

function EnemyPlane:getActionArray()
	return self._actionarray;
end

function EnemyPlane:setPlaneData(data, strongcount)
	self._speed = data.speed or 0;
	self._angle = data.angle or 0;
	self._attack = data.attack or 0;
	self._defensive = data.defensive or 0;
	self._width = data.size.width or 0;
	self._height = data.size.height or 0;
	-- 强化
	if strongcount then
		self._blood = self._blood * strongcount
	end
end

--[[
	移动 1
	静止 2
	自旋转 3
]]
function EnemyPlane:move()
	-- 要看动作决定动作
	if #self._actionarray == 0 then
		return
	end
	
	if self._actionstep == 0 then
		self._actionstep = 1;
	end
	local actionD = self._actionarray[self._actionstep]
	if not actionD then
		return
	end
	--print("self._actionstep = " .. self._actionstep)
	--table.print(actionD)
	if actionD.actionenum == 0 then
		if self._steptime == 0 then
			self._angle = actionD.angle
			self._posX = actionD.startpos.x
			self._posY = actionD.startpos.y
		end
		self._steptime = self._steptime + 0.02
		if self._angle == -90 then
			self._posY = self._posY - self._speed;
			--print("self._posY" .. self._posY)
			if self._posY <= actionD.endpos.y then
				self._actionstep = self._actionstep + 1
				self._steptime = 0
				return
			end
		end
	elseif actionD.actionenum == 2 then
	elseif actionD.actionenum == 3 then
		--print("enemy move action = 3")
		if not self._bFire then
			self._bFire = true;
			self._nFireTime = actionD.firetime or 999
			self._steptime = 0
			self._bulletdata = { 
				bullet = BulletCfg[actionD.bullet].bullettype,
				bulletacttype = BulletTypeCfg[actionD.bulletacttype].actiontype,
			};
		end		
		self._steptime = self._steptime + 0.02
	elseif actionD.actionenum == 4 then
		if self._steptime == 0 then
			self._angle = actionD.angle
			self._posX = actionD.startpos.x
			self._posY = actionD.startpos.y
		end
		if self._angle == -90 then
			self._posY = self._posY - self._speed;
			--print("self._posY" .. self._posY)
			if self._posY <= actionD.endpos.y then
				self._actionstep = self._actionstep + 1
				self._steptime = 0
				return
			end
		end
		if not self._bFire then
			self._bFire = true;
			self._nFireTime = actionD.firetime or 999
			self._steptime = 0
			self._bulletdata = { 
				bullet = BulletCfg[actionD.bullet].bullettype,
				bulletacttype = BulletTypeCfg[actionD.bulletacttype].actiontype,
			};
		end		
		self._steptime = self._steptime + 0.02
	end
	
	if self._steptime >= actionD.ttime then
		self._steptime = 0
		self._bFire = false;
		self._actionstep = self._actionstep + 1
		self._bulletdata = {};
	end
end

function EnemyPlane:getActionStats()
	return { actionstep = self._actionstep, steptime = self._steptime }
end

function EnemyPlane:getCurrBulletData()
	return self._bulletdata;
end

function EnemyPlane:isFire()
	if not self._bFire then
		return false;
	end
	if self._steptime >= self._nFireTime  then
		self._steptime = 0;
		return true;
	end
	return false;
end

function EnemyPlane:judgeOut()
	if self._posX <= -200 or self._posX >= 900 or self._posY <= -200 or self._posY >= 1700 then
		return true;
	end
	return false;
end

--受伤
function EnemyPlane:injured(object)
	local jureBlood = object.attack - self._defensive
	self._blood = self._blood - jureBlood
	local data = {
		bExist = false,
		disblood = 0,
	}
	data.disblood = -jureBlood;
	if self._blood <= 0 then
		data.bExist = false
	else
		data.bExist = true
	end
	return data
end

function EnemyPlane.new(...)
	local instance = {}
	setmetatable(instance, e_plane_mt)
	instance:ctor(...)
	return instance
end

return EnemyPlane
